﻿using System;
using System.Collections.Generic;
using LoanCalculationService.Objects;
using System.Data.Common;
using System.Collections;
using System.Web.UI;
using System.Data;

namespace BLLoanCalculation
{
	/// <summary>
	/// Summary description for LoanCalculation Business Layer
	/// </summary>
	public class BLLoanCalculation
	{

		public static bool checkLoanEligibility(decimal pLoanAmount, decimal pLoanTerm, decimal pPropertyValue, decimal pAnnualdisposableIncome, int pPropertyYearBuilt, out string pErrorMsg)
		{
			bool isValid = true;
			string errorMsg = string.Empty;
			// Validate details
			// Check that all fields have been supplied.
			if ( decimal.Zero.Equals(pLoanAmount) ){ errorMsg += "Loan amount must be supplied. "; isValid = false; }
			if ( decimal.Zero.Equals(pLoanTerm)) { errorMsg += "Loan term must be supplied. "; isValid = false; }
			if ( decimal.Zero.Equals(pPropertyValue)) { errorMsg += "Property value must be supplied. "; isValid = false; }
			if ( decimal.Zero.Equals(pAnnualdisposableIncome)) {errorMsg += "Annual disposable income must be supplied. "; isValid = false; }
			if ( 0 == pPropertyYearBuilt ){ errorMsg += "Property year built must be supplied. "; isValid = false; }
			if (!isValid)
			{
				pErrorMsg = errorMsg;
				return false;
			}

			// Loan Term must be between 5 and 30 years inclusive.
			if (pLoanTerm < 5 || pLoanTerm > 30)
			{
				errorMsg += "Loan Term must be between 5 and 30 inclusive. ";
				isValid = false;
			}
			// Amount of Loan must be between $10,000 and $5 million inclusive.
			if (pLoanAmount < 10000 || pLoanAmount > 5000000)
			{
				errorMsg += "Amount of Loan must be between $10,000 and $5 million inclusive. ";
				isValid = false;
			}

			pErrorMsg = errorMsg;
			return isValid;
		}

		public static decimal CalculateMonthlyLoanRepayment(decimal pLoanAmount, decimal pLoanTerm, decimal pBankIntrestRate, decimal pLoanInsurance, out  string pErrorMsg)
		{
			bool isValid = true;
			string errorMsg = string.Empty;
			// Validate details
			// Check that all fields have been supplied.
			if (decimal.Zero.Equals(pLoanAmount)) { errorMsg += "Loan amount must be supplied. "; isValid = false; }
			if (decimal.Zero.Equals(pLoanTerm)) { errorMsg += "Loan term must be supplied. "; isValid = false; }
			if (decimal.Zero.Equals(pBankIntrestRate)) { errorMsg += "Bank Interest Rate must be supplied. "; isValid = false; }
			if (!isValid)
			{
				pErrorMsg = errorMsg;
				return 0.0M;
			}

			//* A = periodic payment amount
			//* P = amount of principal, net of initial payments, meaning "subtract any down-payments"
			//* i = periodic interest rate
			//* n = total number of payments
			double A = 0;
			double P = Convert.ToDouble( pLoanAmount );
			double i = Convert.ToDouble( pBankIntrestRate);
			double n = Convert.ToDouble( pLoanTerm * 12); // To get mthly payments.

			//A = Convert.ToDecimal( Convert.ToDouble((P * i)) / ( 1 - Math.Pow( Convert.ToDouble(1+i), Convert.ToDouble(-n)) ));
			// brewakdown
			double pxi = P * i;
			double bottom = ((double)1) - (Math.Pow((((double)1) + i), -n));
			double final = pxi / bottom;

			pErrorMsg = errorMsg;
			return Convert.ToDecimal(final);
		}

		internal static decimal CalculateAnnualInsurPremium(int pCreditRating, decimal pLoanAmount)
		{
			if (pCreditRating == 3) return (0.02M*pLoanAmount);
			if (pCreditRating == 4) return (0.01M*pLoanAmount);
			if (pCreditRating == 5) return (0M);
			return -1;
		}
	}
}
 